Skip to main content

SpringBoot browser loading image (No mapping for GET /img/favicon.png) error solution

SpringBoot browser loading image error solution

Hello everyone, I am Inch Iron.

Do you also get the following error in your browser when using idea? Σ( °Д °)Σ

Springboot access static resource error: No mapping for GET /img/favicon.png

It doesn’t matter, I will give you the solution below!

Scenario 1

Check whether the picture path is correct:

Picture path: Pictures under the image folder under static

Here is the local path: just / replaces static

  <img src="/image/用户.jpg"></src>

Note: If there is no static folder, you can check whether the creation failed or recreate other folders.

Scenario 2

The easiest way to check whether there is a problem with meaven dependencies is:

click on the ** in the left frame meaven- click on the life cycle ** - click on ** clean** (try multiple clicks to see the results)

Manual debugging (not commonly used): Click the ** targetfolder ---- clickpom.xml ** to find errors one by one, re-run the project

to view dependencies and find errors one by one :

Scenario 3

The path of the file is inconsistent with the browser recognition, and code needs to be added to resolve it. Right click on config the folder - create MvcConfig class - copy the code over there

Copy code:

code show as below:

static-code1

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
/**
* 静态资源加载设置
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}

}

Students who failed to try the first code can try the following code

static-code2

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyl.learn.kbaserepo.base.utils.EnumModule;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {



/**
* 静态资源加载设置
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
}

Finally, run the project to verify whether it is successful